use std::sync::mpsc::Sender;
use std::sync::{Arc, mpsc, Mutex};
use crate::llog::log_utils::LogInfo;

pub(crate) struct TokioStart {
    //
    sender: Sender<TokioConfig>,
}


impl TokioStart {
    fn build_self() -> Arc<Mutex<Self>> {
        let (tx, rx) = mpsc::channel::<TokioConfig>();
        let runtime = tokio::runtime::Runtime::new();
        std::thread::spawn(move || {
            runtime.unwrap().block_on(async move {
                for rx in rx {
                    jx_config(rx).await;
                }
                println!("异步运行时结束");
            });
        });
        Arc::new(Mutex::new(Self {
            sender: tx,
        }))
    }

    pub(crate) fn get_instance() -> Arc<Mutex<Self>> {
        static mut INSTANCE: Option<Arc<Mutex<TokioStart>>> = None;
        unsafe {
            INSTANCE.get_or_insert_with(|| {
                TokioStart::build_self()
            }).clone()
        }
    }

    pub fn init(&self) {}
}

pub fn send_task(data: TokioConfig) {
    TokioStart::get_instance().lock().unwrap().sender.clone().send(data).unwrap();
}


async fn jx_config(config: TokioConfig) {
    match config {
        TokioConfig::NtpTime => {
            println!("开始ntp任务");
            crate::linit::test().await;
        }
    }
}

pub enum TokioConfig {
    NtpTime
}
